package com.cee.admin.config.mybatisplus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.cee.domain.enums.PlatformEnum;
import com.cee.ds.common.utils.BasePrincipalUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class CommonFieldFillingHandler implements MetaObjectHandler {

    private static final PlatformEnum platformEnum = PlatformEnum.BACKEND;

    @Override
    public void insertFill(MetaObject metaObject) {
        //统一设置创建人，创建时间，创建人名等公共字段。
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "createUserId", Long.class, BasePrincipalUtils.currentUserId());
        this.strictInsertFill(metaObject, "createPlatform", PlatformEnum.class, platformEnum);
        this.strictInsertFill(metaObject, "applicationTime", Date.class, new Date());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        //统一设置修改人，修改时间，修改人名等公共字段。
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
        this.strictUpdateFill(metaObject, "updateUserId", Long.class, BasePrincipalUtils.currentUserId());
        this.strictInsertFill(metaObject, "updatePlatform", PlatformEnum.class, platformEnum);
        this.strictInsertFill(metaObject, "applicationTime", Date.class, new Date());
    }
}
